;
(function (window, angular) {
    'use strict';
    angular.module("Controller.Personal.Password", [])
        .controller("PasswordCtrl", ["$scope", "$state", "ybWidget", "ybHttp", "MESG_TYPE", "UserContextService", "$stateParams", function ($scope, $state, ybWidget, ybHttp, MESG_TYPE, UserContextService, $stateParams) {
            $scope.submitted = false;
            $scope.detailInfo = {
                oldPwd: "",
                newPwd: "",
                confirmPwd: ""
            };

            $scope.saveData = function (isValid) {
                if (!isValid) {
                    $scope.submitted = true;
                    return;
                }

                if ($scope.detailInfo.newPwd !== $scope.detailInfo.confirmPwd) {
                    return;
                }
                var service = "auth";
                var param = {
                    oldPassword: $scope.detailInfo.oldPwd,
                    newPassword: $scope.detailInfo.newPwd,
                    confirmPassWord: $scope.detailInfo.confirmPwd,
                    loginName: $scope.loginname,
                };
                ybHttp.Post(service, $scope.reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    $scope.submitted = false;
                    ybWidget.ShowMessage("操作成功<br>请重新登录", MESG_TYPE.Success, {isHtml: true}).then(function () {
                        UserContextService.Logout();
                    });
                });
            };

            $scope.progressBar = {
                value: '25',
                title: '很低',
                class: 'progress-bar-danger'
            };
            $scope.handleLevelChange = function (val) {
                switch (val) {
                    case 0:
                    case 1:
                        $scope.progressBar = {
                            value: '25',
                            title: '很低',
                            class: 'progress-bar-danger'
                        };
                        break
                    case 2:
                        $scope.progressBar = {
                            value: '45',
                            title: '低',
                            class: 'progress-bar-warning'
                        };
                        break
                    case 3:
                        $scope.progressBar = {
                            value: '65',
                            title: '中',
                            class: 'progress-bar-info'
                        };
                        break
                    case 4:
                        $scope.progressBar = {
                            value: '100',
                            title: '强',
                            class: 'progress-bar-success'
                        };
                        break
                  }
            }

            function getUrlParams(name) {
                var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
                var parm = window.location.hash.split("?")[1];
                if (parm == null) return null;
                var r = parm.match(reg);
                if (r == null) return null;
                return unescape(r[2]);
            }

            (function () {
                var loginnameVal = getUrlParams("loginname");
                $scope.reqUrl = "/auth/password";
                if ($scope.curUser && $scope.curUser.loginName) {
                    $scope.loginname = $scope.curUser.loginName;
                } else if(loginnameVal) {
                    $scope.loginname = loginnameVal;
                    $scope.reqUrl = "/auth/passwordModify";
                }
            })();
        }])
    ;
})(window, window.angular);